package com.derbysoft.dswitch.adapter.ihg.dswitch2.translate;

import com.derbysoft.dswitch.adapter.ihg.common.utils.CollectionUtil;
import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.core.GenericRS;
import com.derbysoft.remote.dswitch.core.ResponseHeader;
import com.derbysoft.remote.dswitch.core.SuccessDTO;
import com.derbysoft.remote.dswitch.hotel.dto.*;
import org.apache.commons.collections.Predicate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Component("ds2HotelMultiAvailRSTranslator")
public class HotelMultiAvailRSTranslator
        implements Translator<Collection<GenericRS<HotelAvailRS>>, GenericRS<HotelMultiAvailRS>, GenericRQ<HotelMultiAvailRQ>> {

    public GenericRS<HotelMultiAvailRS> translate(Collection<GenericRS<HotelAvailRS>> hotelAvailRSs,
                                                  GenericRQ<HotelMultiAvailRQ> hotelMultiAvailRQ) {
        GenericRS<HotelMultiAvailRS> hotelMutiAvailRS = new GenericRS<HotelMultiAvailRS>();
        hotelMutiAvailRS.setHeader(translateResponseHeader(hotelMultiAvailRQ));
        hotelMutiAvailRS.setSuccess(new SuccessDTO());
        HotelMultiAvailRS payLoad = new HotelMultiAvailRS();
        payLoad.setHotelAvailRoomStays(translateHotelAvailRoomStays(hotelAvailRSs, hotelMultiAvailRQ));
        hotelMutiAvailRS.setPayload(payLoad);
        return hotelMutiAvailRS;
    }

    @SuppressWarnings("unchecked")
    private List<HotelAvailRoomStayDTO> translateHotelAvailRoomStays(Collection<GenericRS<HotelAvailRS>> hotelAvailRSs,
                                                                     GenericRQ<HotelMultiAvailRQ> hotelMultiAvailRQ) {
        List<HotelAvailRoomStayDTO> hotelAvailRoomStays = new ArrayList<HotelAvailRoomStayDTO>();
        for (final HotelRefDTO hotelRef : hotelMultiAvailRQ.getPayload().getHotels()) {
            HotelAvailRoomStayDTO hotelAvailRoomStay = new HotelAvailRoomStayDTO();
            hotelAvailRoomStay.setHotelRef(hotelRef);
            GenericRS<HotelAvailRS> hotelAvailRS = (GenericRS<HotelAvailRS>) CollectionUtil.find(hotelAvailRSs, new Predicate() {
                public boolean evaluate(Object o) {
                    return hotelRef.getCode().equals(((GenericRS<HotelAvailRS>) o).getPayload().getHotelAvailRoomStay()
                            .getHotelRef().getCode());
                }
            });
            if (hotelAvailRS != null) {
                hotelAvailRoomStay.setRoomStays(hotelAvailRS.getPayload().getHotelAvailRoomStay().getRoomStays());
            }
            hotelAvailRoomStays.add(hotelAvailRoomStay);
        }
        return hotelAvailRoomStays;
    }

    private ResponseHeader translateResponseHeader(GenericRQ<HotelMultiAvailRQ> hotelMultiAvailRQ) {
        ResponseHeader responseHeader = new ResponseHeader();
        responseHeader.setTaskId(hotelMultiAvailRQ.getHeader().getTaskId());
        return responseHeader;
    }
}
